package com.ytjj.qmyx.admin.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.*;

import javax.annotation.Resource;

import com.ytjj.common.enums.ExceptionOrderClassifyEnum;
import com.ytjj.qmyx.admin.model.excel.OrdersExceptionExcel;
import com.ytjj.qmyx.admin.model.request.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.CopyWriterConstant;
import com.ytjj.common.utils.CopyWriterConfigUtil;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.client.BankerClient;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.Role;
import com.ytjj.qmyx.admin.model.YxOrderExceptionNote;
import com.ytjj.qmyx.admin.model.banker.OrderExceptionNoteReq;
import com.ytjj.qmyx.admin.service.AdminService;
import com.ytjj.qmyx.admin.service.BankerService;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.admin.service.ProductService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import com.ytjj.qmyx.mall.model.Banker;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 店家用户管理
 */
@RestController
@RequestMapping("/banker")
@Slf4j
public class BankerController {
    @Autowired
    private BankerService bankerService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MemberService memberService;
    @Resource
    private CopyWriterConfigUtil copyWriterConfigUtil;
    @Autowired
    private BankerClient bankerClient;
    @Autowired
    private ProductService productService;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    /**
     * 查询子账号
     * @param bankerRequest
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestBody BankerRequest bankerRequest){
        Banker banker = memberService.getBanker();
        bankerRequest.setBankerId(banker.getId());
        CommonPage<Admin> pageList = bankerService.getChildren(bankerRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 创建子账号
     * @param admin
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Admin admin){
        Banker banker = memberService.getBanker();
        admin.setBankerId(banker.getId());
        Admin data = bankerService.create(admin);
        if(data==null){
            return CommonResult.failed(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.PHONE_HOMOLOGOUS_NUMBER_EXIST));
        }
        return CommonResult.success(data);
    }

    /**
     * 修改子账号
     * @param admin
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody Admin admin){
        int count = bankerService.update(admin);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据主键查询子账号实体
     * @param id
     * @return
     */
    @GetMapping("/getBanker")
    public CommonResult getBanker(@RequestParam Integer id){
        Admin admin = bankerService.getBanker(id);
        return CommonResult.success(admin);
    }

    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam Integer id){
        int count = bankerService.delete(id);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据子账号id查询角色信息(子账号id就是adminId)
     * @param id
     * @return
     */
    @GetMapping(value = "/getRoleById")
    public CommonResult<List<Role>> getRoleList(@RequestParam Integer id) {
        List<Role> roleList = adminService.getRoleListByAdminId(id);
        return CommonResult.success(roleList);
    }

    /**
     * 给子账号分配角色
     * @param updateRoleRequest
     * @return
     */
    @PostMapping(value = "/role/update")
    public CommonResult updateRole(@RequestBody UpdateRoleRequest updateRoleRequest) {

        int count = adminService.updateRole(updateRoleRequest);
        if (count >= 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
    /**
     * 审核供应商主体信息
     * @param request
     * @return
     */
    @PostMapping("/checkBanker")
    public CommonResult checkBanker(@RequestBody BankerCheckStatusRequest request) {
        return bankerClient.checkBanker(request);
    }

    /**
     * 审核供应商店铺信息
     * @param request
     * @return
     */
    @PostMapping("/checkBankerSite")
    public String checkBankerSite(@RequestBody BankerCheckStatusRequest request) {
        return bankerService.checkBankerSite(request);
//        return bankerClient.checkBankerSite(request);
    }

    /**
     * 审核供应商品牌信息
     * @param request
     * @return
     */
    @PostMapping("/checkBankerBrand")
    public String checkBankerBrand(@RequestBody BankerCheckStatusRequest request) {
        return bankerService.newCheckBankerBrand(request);
//        return bankerClient.checkBankerBrand(request);
    }

    /**
     * 释放供应商主体信息
     * @param request
     * @return
     */
    @RequiresPermissions("banker:releaseBanker")
    @PostMapping("/releaseBanker")
    public String releaseBanker(@RequestBody BankerUpdateReleaseRequest request) {
        return bankerService.newReleaseInfo(request);
    }

    /**
     * 释放供应商店铺信息
     * @param request
     * @return
     */
    @PostMapping("/releaseBankerSite")
    public String releaseBankerSite(@RequestBody BankerUpdateReleaseRequest request) {
        return bankerService.newCheckBankerSite(request);
//        return bankerClient.releaseSite(request);
    }

    /**
     * 释放供应商品牌信息
     * @param request
     * @return
     */
    @PostMapping("/releaseBankerBrand")
    public String releaseBankerBrand(@RequestBody BankerUpdateReleaseRequest request) {
        return bankerService.newReleaseBankerBrand(request);
//        return bankerClient.releaseBrand(request);
    }

    /**
     * 供应商品牌下架
     * @param request
     * @return
     */
    @PostMapping("/putOnBankerBrand")
    public String putOnBankerBrand(@RequestBody BankerUpdatePutOnRequest request) {
        return productService.newUpdateProductStatusByBrandId(request);
//        log.info("===供应商品牌上下架==={}", request);
//        CommonResult result = productService.updateProductStatusByBrandId(request.getId(), request.getStatus() == 0 ? 2 : 1);
//        if (ResultCode.SUCCESS.getCode() != result.getCode()) {
//            return result;
//        }
//        return bankerClient.putOnBrand(request);
    }

    /**
     * 修改供应商品牌名称
     * @param request
     * @return
     */
    @PostMapping("/updateBrandName")
    public String updateBrandName(@RequestBody BrandUpdateNameRequest request) {
        return bankerService.newUpdateBrandName(request);
//        return bankerClient.updateBrandName(request);
    }

    /**
     * 查询商品对应的供应商下的所有品牌列表
     * @param bankerId
     * @return
     */
    @GetMapping("/queryBrandList")
    public String queryBrandList(@RequestParam("bankerId") Integer bankerId) {
        Map<String, String> map = Maps.newHashMap();
        map.put("bankerId", String.valueOf(bankerId));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/queryBankerBrandList", map, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    /**
     * 重置供应商密码
     * @param request
     * @return
     */
    @PostMapping("/resetPwd")
    public CommonResult resetBankerPwd(@RequestBody BrandUpdateNameRequest request) {
        return bankerService.resetPwd(request);
//        return bankerClient.resetPwd(request);
    }

    //----------------------------------==异常订单跟踪页==----------------------------------------------
    /**
     * 供应商异常订单跟踪页--备注列表
     * @param id
     * @return
     */
    @GetMapping("/selectNote")
    public CommonResult selectNote (@RequestParam Integer id){
        Map<String, String> map = new HashMap();
        map.put("id", String.valueOf(id));
        String result = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/selectNote", map, supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
        return commonResult;
//        return bankerClient.selectNote(id);
    }


    /**
     * 供应商异常订单跟踪页--批量备注
     * @param ids
     * @param content
     * @return
     */
    @GetMapping ("/insertNote")
    public CommonResult insertNote(String ids,String content){
        Admin admin = UserUtil.getAdmin();
        Map<String,String> req = new HashMap<>();
        req.put("ids",ids);
        req.put("content",content);
        req.put("adminName",admin.getName());
        String result = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/insertNote",req, supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
        return commonResult;
    }
    
    

    
    
}
